import 'package:flutter/material.dart';
import '../../package/color/color_constant.dart';
import '../../package/toast/BotToastCall.dart';

import '../package_model_comm.dart';
import 'MultiNormalSelectChip.dart';

/*
 * 2020.1.3
 * DingHL
 * 筛选组件
 * 筛选组件页面调用示例
 body:SingleChildScrollView(
          physics:NeverScrollableScrollPhysics() ,//设置禁止滑动
          child: Column(
            children: <Widget>[
              Container(
                height: 40,
                child: Text("这里还有组件内容"),
              ),
              //筛选
              Screen(),
            ],
          ),
        ),
        注意：要用SingleChildScrollView（）包起来，要不然会超出边界
 */
class Screen extends StatefulWidget {
//  ValueChanged<Map<String,String>> onTapReset;//选中后回调
  ValueChanged<Map<String,String>> onTapConfirm;//选中后回调
  GestureTapCallback onTapReset;//重置按钮点击事件
  GestureTapCallback onTapSearch;//搜索点击事件
//  GestureTapCallback onTapConfirm;//确认按钮点击事件
  bool isSelect;//是否点击筛选(默认false不展开)
  bool isScreen;//是否筛选（true 确定筛选，false没有确定筛选）
  int selectedIndex; //组员列表选择的索引号(为-1时代表未选择)
  List<BaseModel> taskAllList ;//全部任务类型数组
  List<String> taskSelectList;//任务类型默认选择数组
  //组长全部数组
  List<BaseModel> memberAllList;
  List<String> memberSelectList;//组长选中数组


  Screen({
    Key  key,
    this.onTapReset,
    this.onTapConfirm,
    this.onTapSearch,
    this.isSelect:false,
    this.isScreen:false,
    this.selectedIndex:-1,
    this.taskAllList,
    this.taskSelectList,
    this.memberAllList,
    this.memberSelectList

  }): super(key:key);


  @override
  _ScreenState createState() => _ScreenState();
}

class _ScreenState extends State<Screen> {

  //全部任务类型数组
  List<BaseModel> taskAllList = [];

  List<String> taskSelectList = [];//任务类型默认选择数组

  //组长全部数组
  List<BaseModel> memberAllList = [];
  List<String> memberSelectList = [];//组长选中数组

  bool isSelect = false;//是否点击筛选(默认false不展开)
  bool isScreen = false;//是否筛选（true 确定筛选，false没有确定筛选）
//
//  int selectedIndex = -1; //组员列表选择的索引号(为-1时代表未选择)

  ///数组转字符串
  getStringsForList(List data){
    String str = '';
    if(data.length>0){
      for(String item in data){
        String string = '';
        if(str.length>0){
          string = ',';
        }
        str = str + string + item;
      }

    }
    return str;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    isSelect = widget.isSelect;
    isScreen = widget.isScreen;

    if(widget.taskAllList != null){
      taskAllList = widget.taskAllList;
      if(widget.taskSelectList != null){
        taskSelectList = widget.taskSelectList;
      }else{
        taskSelectList = [];
      }
    }else{
//      taskAllList = [];
      taskAllList = [];
    }
    if(widget.memberAllList != null){
      memberAllList = widget.memberAllList;
      if(widget.memberSelectList != null){
        memberSelectList = widget.memberSelectList;
      }else{
        memberSelectList = [];
      }
    }else{
//      memberAllList = [];
      memberAllList=[];
    }

  }



  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
//        widget.isScreen?
//        //内容为空页面
//        Container(
//          margin: EdgeInsets.only(top: 40),
//          child: NotContent(),
//        ):Container(),
        //筛选展开选择块
        isSelect == true?
        InkWell(
          child: Container(
            margin: EdgeInsets.only(top: 40),
//                color: Colors.red,
            color: Color(0xFFdbdbdb),
            height: MediaQuery.of(context).size.height,
            child: Stack(
              children: <Widget>[
                Stack(
                  children: <Widget>[
                    //筛选条件内容选择块
                    InkWell(
                      child: Container(
                        color: Colors.white,
//                          color: Colors.red,
                        height: 365,
//                            margin: EdgeInsets.only(bottom:150,),
                        child: ListView(
                          children: <Widget>[
                            //任务类型
                            Container(
                              margin: EdgeInsets.only(left: 12.0, bottom: 12.0,right: 12.0),
                              child: Column(
                                children: <Widget>[
                                  Container(
                                    alignment: Alignment.centerLeft,
                                    margin: EdgeInsets.only(top: 12.0, bottom: 12.0),
                                    child: RichText(
                                      text: TextSpan(
                                          children: <TextSpan> [
                                            TextSpan(
                                                text: '任务类型',
                                                style: TextStyle(color: ColorConstant.HY_Title_Black, fontSize: 15)
                                            ),
                                            TextSpan(
                                                text: ' ${taskSelectList.length}',
                                                style: TextStyle(color: taskSelectList.length>0?ColorConstant.HY_Blue:ColorConstant.HY_Desc_Gray, fontSize: 15)
                                            )
                                          ]
                                      ),
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.centerLeft,
                                    margin: EdgeInsets.only(bottom: 10.0),
                                    child: MultiNormalSelectChip(
                                      taskAllList,
                                      selectList: taskSelectList,
                                      onSelectionChanged: (taskSelectList) {
                                        setState(() {
                                          taskSelectList = taskSelectList;
                                          String str = getStringsForList(taskSelectList);
                                          print('任务类型：$str');
                                        });
                                      },
                                    ),
                                  )
                                ],
                              ),
                            ),
                            Container(
//                              margin: EdgeInsets.all(12.0),
                              margin: EdgeInsets.only(left: 12,bottom: 12,right: 12),
                              child: RichText(
                                text: TextSpan(
                                    children: <TextSpan> [
                                      TextSpan(
                                          text: '组长',
                                          style: TextStyle(color: ColorConstant.HY_Title_Black, fontSize: 15)
                                      ),
                                      TextSpan(
                                          text: ' ${memberSelectList.length}',
                                          style: TextStyle(color: memberSelectList.length>0?ColorConstant.HY_Blue:ColorConstant.HY_Desc_Gray, fontSize: 15)
                                      )
                                    ]
                                ),
                              ),
                            ),
                            Divider(height: 1,color: Colors.grey[200],),
                            //组长列表
                            Container(
                              child: Column(
                                children: <Widget>[
                                  Container(
                                    height:180,
                                    child: ListView.builder(
                                      itemCount: memberAllList.length,
//                                    physics: NeverScrollableScrollPhysics(),//设置列表不能滑动
                                      itemBuilder: (context, index) {
                                        return InkWell(
                                          child: Container(
                                            //列表下划线（边框）
                                            decoration: BoxDecoration(
                                                border: Border(bottom: BorderSide(color: Colors.grey[200],width: 0.0))
                                            ),
                                            child: Row(
                                              children: <Widget>[
                                                Expanded(
                                                  child:Container(
                                                    width: width,
                                                    padding: EdgeInsets.only(left: 13, right: 13, top: 15, bottom: 15,),
                                                    alignment: Alignment.centerLeft,
//                                                color: Colors.white,
                                                    child: Text(
                                                      memberAllList[index].name,
                                                      style: TextStyle(
                                                        fontSize: 14,
                                                        //单选selectedIndex == index
                                                        //多选
                                                        color: memberSelectList.indexOf(memberAllList[index].name) != -1?Color(0xff55B3E4):Color(0xff666666),
                                                      ),
                                                      textAlign: TextAlign.start,
                                                    ),
                                                  ),
                                                  flex: 5,
                                                ),
//                                                selectedIndex == index?单选
                                                //多选
                                                memberSelectList.indexOf(memberAllList[index].name) != -1?
                                                Expanded(
                                                  child: Container(child: Icon(Icons.check,color: Color(0xff55B3E4),size: 18,),),
                                                  flex: 1,
                                                ):Container(),
                                              ],
                                            ),
                                          ),
                                          onTap: (){
                                            setState(() {
                                              //如果已选中（单选反选）
                                              if(widget.selectedIndex == index){
                                                widget.selectedIndex = -1;
                                              }else{
                                                widget.selectedIndex = index;//把选择的子项赋值
                                              }
                                              //多选
                                              if(memberSelectList.indexOf(memberAllList[index].name) == -1){
                                                memberSelectList.add(memberAllList[index].name);//把选择组长人员以数组方式存储
                                                print("数组添加后的值：");
                                                print(memberSelectList);
                                              }else{
                                                // 移除数组中的元素
                                                memberSelectList.remove(memberAllList[index].name);
                                                print("移除数组后的值：");
                                                print(memberSelectList);
                                              }

                                            });
                                          },
                                        );
                                      },
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      onTap: (){},
                    ),
                    //重置确定按钮
                    Positioned(
//                            bottom: 150,
                        bottom: -1,
                        child: Container(
                          color: Colors.white,
                          height: 45,
                          child: Row(
                            children: <Widget>[
                              Container(
                                width: MediaQuery.of(context).size.width/2,
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  border: Border(top: BorderSide(width: 1, color: ColorConstant.HY_Blue)),
                                ),
                                //重置
                                child: FlatButton(
                                  onPressed: (){
                                    setState(() {
                                      taskSelectList = [];//重置设置任务类型为空
                                      memberSelectList = [];//重置设置组长选中数组为空
                                      isScreen = false;//重置设置筛选未确定
                                    });
                                  },
//                                  onPressed: widget.onTapReset,//重置按钮点击事件
                                  child: Text('重置', style: TextStyle(color: ColorConstant.HY_Blue, fontSize: 17),),
                                ),
                              ),
                              Container(
                                width: MediaQuery.of(context).size.width/2,
                                color: ColorConstant.HY_Blue,
                                //确定
                                child: FlatButton(
                                  onPressed: (){
                                    print("taskSelectList.length的长度"+taskSelectList.length.toString());
                                    print("memberSelectList.length的长度"+memberSelectList.length.toString());
                                    if(taskSelectList.length>0 && memberSelectList.length >0 ){
                                      setState(() {
                                        isSelect = false;//设置筛选内容关闭
                                        isScreen = true;//设置状态为确定
                                      });
                                    }else{
                                      if(taskSelectList.length == 0 || memberSelectList.length == 0 ){
                                        BotToastCall.ShowMessage( "请选择完整");//普通文本 默认2秒 底下弹出
                                      }else{
                                        setState(() {
                                          isSelect = true;//设置筛选内容打开
                                          isScreen = false;//设置状态为未确定
                                        });
                                        BotToastCall.ShowMessage( "请选择要查询的内容");//普通文本 默认2秒 底下弹出
                                      }

                                    }
                                    String taskSelectListStr = "";
                                    String memberSelectListStr = "";
                                    //将选择的id 进行拼接
                                    taskSelectList.forEach((f){
                                      taskSelectListStr += f + ",";
                                    });
                                    memberSelectList.forEach((f){
                                      memberSelectListStr += f + ",";
                                    });
                                    Map<String,String> map  = new Map();
                                    if(taskSelectListStr != null && taskSelectListStr != "") {
                                      map["taskSelectList"] = taskSelectListStr.substring(0,taskSelectListStr.length-1);
                                    }
                                    if(memberSelectListStr != null && memberSelectListStr != "") {
                                      map["memberSelectList"] = memberSelectListStr.substring(0,memberSelectListStr.length-1);
                                    }
                                    widget.onTapConfirm(map);
                                  },
//                                  onPressed: widget.onTapConfirm,
                                  child: Text('确定', style: TextStyle(color: Colors.white, fontSize: 17),),
                                ),
                              )
                            ],
                          ),
                        )
                    )
                  ],
                )
              ],
            ),
          ),
          onTap: (){
            setState(() {
              isSelect = false;//设置筛选内容关闭
            });
          },
        )
            :Container(),
        //筛选-搜索
        Container(
          height: 40,
          decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                  bottom: BorderSide(width: 1.0, color: Colors.grey[300])
              )
          ),
          child: Container(
            height: 40,
            margin: EdgeInsets.only(left: 12.0, right: 12.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                //筛选
                InkWell(
                  onTap: (){
                    //筛选如果展开，设置关闭，否则设置展开
                    if(isSelect == true){
                      //筛选点击状态
                      setState(() {
                        isSelect = false;//设置点击筛选状态
                      });
                    }else{
                      //筛选点击状态
                      setState(() {
                       isSelect = true;//设置点击筛选状态
                      });
                    }


                  },
                  child: Container(
                    child: Row(
                      children: <Widget>[
                        Text('筛选', style: TextStyle(color: isScreen?Color(0xFF419efe):Color(0xFF333333), fontSize: 14),),
                        Container(
                          margin: EdgeInsets.only(left: 5),
                          child: Image.asset(isScreen?'images/package/screen_select@2x.png':'images/package/screen_nor@2x.png', width: 10, height: 10,),
                        )
                      ],
                    ),
                  ),
                ),
                //搜索
                InkWell(
                  onTap:widget.onTapSearch,
                  child: Container(
                    child: Row(
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(right: 10),
                          child: SizedBox(width: 1, height: 20, child: Container(color: Color(0xFFdbdbdb),),),
                        ),
                        Text('搜索', style: TextStyle(color: Color(0xFF333333), fontSize: 14),)
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
        ),

      ],
    );
  }

}


